﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;

using Please_starve.Common.Managers;

namespace Please_starve.Sprites
{
    public enum SpriteAngle
    {
        Vertical,
        Horizontal
    }

    public class Sprite3D
    {
        public readonly int Id;
        public Texture2D Texture;

        public Sprite3DTextureInformation Sprite3DTextureInfo;
        private Vector3[] Body;

        public Sprite3D(int id, SpriteAngle angle, Sprite3DTextureInformation sprite)
        {
            this.Id = id;
            this.Sprite3DTextureInfo = sprite;
            this.Texture = Resources.GetTexture("Sprite3D/" + sprite.assetName);

            this.Body = initBody(angle);

            if (Sprite3DTypes.Sprites[id] != null)
            {
                throw new Exception();
            }
            Sprite3DTypes.Sprites[id] = this;
        }

        public VertexPositionNormalTexture[] UpdateVertex(Vector2[] texturePosition)
        {
            VertexPositionNormalTexture[] verts = new VertexPositionNormalTexture[6];

            verts[0] =
                new VertexPositionNormalTexture(
                Body[1], Body[0], texturePosition[0]);
            verts[1] =
                new VertexPositionNormalTexture(
                Body[2], Body[0], texturePosition[2]);
            verts[2] =
                new VertexPositionNormalTexture(
                Body[3], Body[0], texturePosition[1]);
            verts[3] =
                new VertexPositionNormalTexture(
                Body[2], Body[0], texturePosition[2]);
            verts[4] =
                new VertexPositionNormalTexture(
                Body[4], Body[0], texturePosition[3]);
            verts[5] =
                new VertexPositionNormalTexture(
                Body[3], Body[0], texturePosition[1]);
            return verts;
        }

        private Vector3[] initBody(SpriteAngle angle)
        {
            float height = 1;
            float width = Texture.Width / Texture.Height; //om höjden är 1 så skalar vi bredden efter höjden.

            switch (angle)
            {
                case SpriteAngle.Vertical:
                    return Body = new Vector3[5]
                           {
                               new Vector3(0.0f, 0.0f, 1.0f),
                               new Vector3(0, height / Sprite3DTextureInfo.rows, 0),
                               new Vector3(0, 0, 0),
                               new Vector3(width / Sprite3DTextureInfo.coloumns, height / Sprite3DTextureInfo.rows, 0),
                               new Vector3(width / Sprite3DTextureInfo.coloumns, 0, 0),
                           };
                case SpriteAngle.Horizontal:
                    return Body = new Vector3[5]
                           {
                               new Vector3(0.0f, 1.0f, 0.0f),
                               new Vector3(0, 0, 0),
                               new Vector3(0, 0, height / Sprite3DTextureInfo.rows),
                               new Vector3(width / Sprite3DTextureInfo.coloumns, 0, 0),
                               new Vector3(width / Sprite3DTextureInfo.coloumns, 0, height / Sprite3DTextureInfo.rows),
                           };
                default:
                    throw new NotImplementedException();
            }
        }
    }
}